//
//  B_MerchantAllOrderVC.m
//  DreamOf
//
//  Created by 薛小刚 on 2018/8/29.
//  Copyright © 2018年 薛小刚. All rights reserved.
//

#import "B_MerchantAllOrderVC.h"
#import "B_MerchantOrderListTVC.h"
#import "B_TwoButtonRightFooterView.h"
#import "B_OrderDerailVC.h"
#import "B_UserOrderListModel.h"

@interface B_MerchantAllOrderVC ()
@property (nonatomic, strong) BaseTableView * mainView;
@end

@implementation B_MerchantAllOrderVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view = self.mainView;
    [self getData:YES];
}

- (BaseTableView *)mainView {
    if (!_mainView) {
        WeakSelf(weSelf, self);
        _mainView = [[BaseTableView alloc] init];
        
        [_mainView setTableViewFrameWithTop:5 left:0 right:0 bottom:0];
        [_mainView.tableView registerNib:[UINib nibWithNibName:@"B_MerchantOrderListTVC" bundle:nil] forCellReuseIdentifier:@"B_MerchantOrderListTVC"];
        _mainView.numberOfRowsBlock = ^NSInteger(UITableView *tableView, NSInteger section) {
            return 1;
        };
        _mainView.heightForRowBlock = ^CGFloat(UITableView *tableView, NSIndexPath *indexPath) {
            return 200;
        };
        _mainView.numberOfSectionsBlock = ^NSInteger(UITableView *tableView) {
            return weSelf.dataArray.count;
        };
        _mainView.cellForRowBlock = ^UITableViewCell *(UITableView *tableView, NSIndexPath *indexPath) {
            B_UserOrderListModel * model = weSelf.dataArray[indexPath.section];
            B_MerchantOrderListTVC * cell = [tableView dequeueReusableCellWithIdentifier:@"B_MerchantOrderListTVC" forIndexPath:indexPath];
            cell.B_merchantName.text = model.nickName;//@"山西特产专营店";
            cell.B_orderType.text = [model getUserOrderType:model.type];
            [cell.B_goodsImageView sd_setImageWithURL:[NSURL URLWithString:model.goodsPic]];
            cell.B_goodsNumber.text = [NSString stringWithFormat:@"x %@", model.goodsNum];
            cell.B_goodsTitle.text = model.goodsName;
            cell.B_goodsPrice.text = [NSString stringWithFormat:@"¥%@", model.goodsPrice];
            cell.B_goodsContent.text = [NSString stringWithFormat:@"共计%@件, 合计¥%.2f", model.goodsNum, [model.goodsPrice floatValue] * [model.goodsNum intValue]];
            return cell;
        };
        
        /**
         TODO:didSelectRow

         @param tableView tableview
         @param indexPath indesPath
         @return 返回
         */
        _mainView.didSelectRow = ^(UITableView *tableView, NSIndexPath *indexPath) {
            B_UserOrderListModel * model = weSelf.dataArray[indexPath.section];
            B_OrderDerailVC * detail = [[B_OrderDerailVC alloc] init];
            detail.B_OrderId = model.id;
            [weSelf pushNextViewController:detail];
        };
        
        _mainView.heightForHeaderBlock = ^CGFloat(UITableView *tableView, NSInteger section) {
            return 10;
        };
        _mainView.viewForHeaderBlock = ^UIView *(UITableView *tableView, NSInteger section) {
            return nil;
        };
        
        _mainView.heightForFooterBlock = ^CGFloat(UITableView *tableView, NSInteger section) {
            B_UserOrderListModel * model = weSelf.dataArray[section];
            if ([model.type isEqualToString:@"3"]) {
                return 10;
            } else {
                return 50;
            }
        };
        _mainView.viewForFooterBlock = ^UIView *(UITableView *tableView, NSInteger section) {
            B_UserOrderListModel * model = weSelf.dataArray[section];
            
            if ([model.type isEqualToString:@"1"]) {
                B_TwoButtonRightFooterView * footer = [B_TwoButtonRightFooterView initB_TwoButtonRightFooterView];
                [footer.B_leftButton setTitle:@"取消订单" forState:UIControlStateNormal];
                [footer.B_rightButton setTitle:@"立即付款" forState:UIControlStateNormal];
                footer.B_twoButtonAction = ^(UIButton *btn) {
                    if (btn.tag == 1) {
                        //TODO:取消订单
                    } else if (btn.tag == 2) {
                        //TODO:立即支付
                    }
                };
                return footer;
            } else if ([model.type isEqualToString:@"2"]) {
                B_TwoButtonRightFooterView * footer = [B_TwoButtonRightFooterView initB_TwoButtonRightFooterView];
                footer.B_leftButton.hidden = YES;
                [footer.B_rightButton setTitle:@"确认收货" forState:UIControlStateNormal];
                footer.B_twoButtonAction = ^(UIButton *btn) {
                    if (btn.tag == 1) {

                    } else if (btn.tag == 2) {
                        //TODO:确认收货
                    }
                };
                return footer;
            }
            return nil;
        };
        
        [_mainView setTableViewHeaderFresh:^{
            [weSelf getData:YES];
        }];
        [_mainView setTableViewFooterFresh:^{
            [weSelf getData:NO];
        }] ;
    }
    return _mainView;
}

- (void) getData:(BOOL) isRefresh {
    if (isRefresh) {
        self.index = 1;
    } else {
        self.index ++;
    }
    [NetworkingManager getNetworkRequestWithUrlString:[NSString stringWithFormat:@"/yearnfor/inter/order/selectUserOrder/%@/4/%ld/10", getUserId(), self.index] parameters:nil isCache:isRefresh succeed:^(id data) {
        [self.mainView endRefresh];
        if (isRefresh) {
            [self.dataArray removeAllObjects];
        }
        if ([data[@"code"] isEqualToString:@"20010"]) {
            for (NSDictionary * dictionary in data[@"values"]) {
                B_UserOrderListModel * model = [B_UserOrderListModel mj_objectWithKeyValues:dictionary];
                [self.dataArray addObject:model];
            }
            [self.mainView.tableView reloadData];
        } else {
            [KeyWindow makeToast:data[@"message"]];
        }
    } fail:^(NSError *error) {
        [self.mainView endRefresh];
        [KeyWindow makeToast:@"服务器异常"];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
